home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 5.4 KB | 229 lines |
- /*
- * @(#)HttpServerHandler.java 1.20 97/05/22
- *
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.http;
-
- import javax.servlet.*;
- import java.net.*;
- import java.io.*;
- import sun.servlet.ServletConnection;
-
- /**
- * This class represents a connection handler in the servlet server.
- *
- * @version 1.20, 05/22/97
- * @author David Connelly
- */
- public
- class HttpServerHandler implements Runnable, ServletConnection {
- /**
- * The server for this handler.
- */
- protected HttpServer server;
-
- /**
- * The servlet request.
- */
- protected final HttpRequest req = new HttpRequest();
-
- /**
- * The servlet response.
- */
- protected final HttpResponse res = new HttpResponse();
-
- /**
- * The current socket connection.
- */
- protected Socket socket;
-
- /**
- * Temporary buffer for file requests.
- */
- protected byte[] buf = new byte[512];
-
- /**
- * The URL prefix for invoking servlets.
- */
- protected static String PREFIX = "/servlet/";
-
- /**
- * The servlet URL prefix length.
- */
- protected static int PREFIX_LEN = PREFIX.length();
-
- /**
- * Creates a new handler for the specified server.
- */
- protected HttpServerHandler(HttpServer server) {
- this.server = server;
- }
-
- /**
- * Runs the connection handler.
- */
- public void run() {
- Socket s;
- while ((s = (Socket)server.getConnection()) != null) {
- try {
- socket = s;
- handleConnection(s);
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * Handles a single connection from the client.
- * @param s the connection socket
- */
- protected void handleConnection(Socket s) throws IOException {
- req.init(this);
- res.init(this);
- while (req.next()) {
- res.next();
- if (req.isFullRequest()) {
- res.setProtocol("HTTP/1.0");
- }
- res.setHeader("Server", server.name);
- res.setKeepAlive(req.getKeepAlive());
- try {
- sendResponse(req, res);
- } catch (Exception e) {
- if (res.getTotalBytes() == 0) {
- try {
- res.sendError(res.SC_INTERNAL_SERVER_ERROR);
- } catch (IOException ee) {
- // eat it!
- }
- }
- e.printStackTrace();
- }
- req.finish();
- res.finish();
- if (!res.getKeepAlive()) {
- break;
- }
- }
- }
-
- /**
- * Sends a response to the client.
- * @exception ServletException if thrown by the servlet
- */
- protected void sendResponse(HttpRequest req, HttpResponse res)
- throws ServletException, IOException
- {
- MessageBytes path = req.getRequestPath();
- if (path.startsWith(PREFIX)) {
- String name = parsePath(path, req);
- if (name != null) {
- // invoke servlet
- Servlet s = server.getServlet(name);
- if (s != null) {
- s.service(req, res);
- return;
- }
- System.err.println("Servlet not found: " + name);
- }
- } else {
- String m = req.getMethod();
- if (m.equals("GET") || m.equals("HEAD")) {
- res.sendError(res.SC_FORBIDDEN,"Will not serve files, "+
- "only servlets");
- return;
- }
- }
- res.sendError(res.SC_NOT_FOUND);
- }
-
- /**
- * Parses a servlet path. Returns the name of the servlet or null if
- * the path was invalid.
- */
- protected String parsePath(MessageBytes path, HttpRequest req) {
- byte[] b = path.getBytes();
- int off = path.getOffset() + PREFIX_LEN;
- int len = path.getLength() - PREFIX_LEN;
- int pos;
- for (pos = off; pos < off + len && b[pos] != '/'; pos++) ;
- if (pos < off + len) {
- req.setPathInfo(b, pos, off + len - pos);
- } else {
- pos = off + len;
- }
- String name = new String(b, 0, off, pos - off);
- off -= PREFIX_LEN;
- req.setServletPath(b, off, pos - off);
- return name;
- }
-
- /**
- * Returns the host name of the server.
- */
- public String getServerName() {
- return server.host;
- }
-
- /**
- * Returns the post number of the server.
- */
- public int getServerPort() {
- return server.port;
- }
-
- /**
- * Returns the local port of the socket connection.
- */
- public String getRemoteHost() {
- return socket.getInetAddress().getHostName();
- }
-
- /**
- * Returns the remote address of the socket connection.
- */
- public String getRemoteAddr() {
- return socket.getInetAddress().getHostAddress();
- }
-
- /**
- * Returns the translated path for the specified virtual path.
- */
- public String getRealPath(String path) {
- return server.getRealPath(path);
- }
-
- /**
- * Returns the input stream for reading from the connection.
- */
- public InputStream getInputStream() throws IOException {
- return socket.getInputStream();
- }
-
- /**
- * Returns the output stream for writing to the connection.
- */
- public OutputStream getOutputStream() throws IOException {
- return socket.getOutputStream();
- }
- }
-